package online.yangcloud.web.service.meta.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.mybatisflex.core.query.QueryWrapper;
import online.yangcloud.common.annotation.TimeConsuming;
import online.yangcloud.common.common.AppConstants;
import online.yangcloud.common.enumration.YesOrNoEnum;
import online.yangcloud.common.tools.ExceptionTools;
import online.yangcloud.common.tools.GlobalTools;
import online.yangcloud.common.tools.SystemTools;
import online.yangcloud.core.mapper.GlobalMapper;
import online.yangcloud.core.mapper.UserMapper;
import online.yangcloud.core.model.entity.UserEntity;
import online.yangcloud.web.service.meta.UserMetaService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author zhuboyang
 * @since 2023年06月12 15:28:21
 */
@Service
@Transactional(rollbackFor = Exception.class)
@TimeConsuming
public class UserMetaServiceImpl implements UserMetaService {

    @Resource
    private GlobalMapper globalMapper;

    @Resource
    private GlobalTools globalTools;

    @Override
    public void insertUser(UserEntity user) {
        int updateResult = globalMapper.acquireUser().insertWithPk(user);
        if (updateResult == 0) {
            ExceptionTools.businessLogger();
        }
    }

    @Override
    public void updateSpaceSize(UserEntity user, Long spaceSize) {
        // 增加空间使用量
        long usedSpaceSize = user.getUsedSpaceSize() + spaceSize;
        user.setUsedSpaceSize(usedSpaceSize);

        // 更新 redis 中的登录信息
        globalTools.acquireRedis().expire(AppConstants.Account.LOGIN_TOKEN + SystemTools.getHeaders().getAuthorization(),
                JSONUtil.toJsonStr(user),
                AppConstants.Account.LOGIN_SESSION_EXPIRE_TIME,
                TimeUnit.MINUTES
        );
    }

    @Override
    public void updateSpaceSize(UserEntity user) {
        updateUser(user);
    }

    @Override
    public void updateUser(UserEntity user) {
        user.setUpdateTime(DateUtil.date().getTime());
        int updateResult = globalMapper.acquireUser().update(user);
        if (updateResult == 0) {
            ExceptionTools.businessLogger();
        }
    }

    @Override
    public UserEntity queryFirstUser() {
        UserMapper databaseUserMapper = globalMapper.acquireUser();
        return databaseUserMapper.selectOneByQuery(QueryWrapper.create()
                .orderBy(UserEntity::getCreateTime).asc()
                .limit(0, 1));
    }

    @Override
    public UserEntity queryUserById(String id) {
        return globalMapper.acquireUser().selectOneByQuery(QueryWrapper.create()
                .where(UserEntity::getId).eq(id)
                .and(UserEntity::getCreateTime).eq(YesOrNoEnum.NO.code()));
    }

    @Override
    public UserEntity queryUserByEmail(String email) {
        return globalMapper.acquireUser().selectOneByQuery(QueryWrapper.create()
                .where(UserEntity::getEmail).eq(email)
                .and(UserEntity::getIsDelete).eq(YesOrNoEnum.NO.code()));
    }

}
